package com.skt.common_hr.fudraw.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.fudraw.FudrawFacade;
	import com.skt.common_hr.fudraw.model.FudrawWriteProxy;
	import com.skt.common_hr.fudraw.model.NoticeViewProxy;
	import com.skt.common_hr.fudraw.view.components.fudrawWrite;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class FudrawWriteMediator extends CommonMediator
	{
		public static const NAME:String = "FudrawWriteMediator";

		private const MODE_WAITING:String = "modeWaiting";
		private const MODE_WRITING:String = "modeWriting";
		private const MODE_MODIFY:String = "modeModify";

		private var _mode:String = MODE_WAITING;
		private var _bWaitingSave:Boolean = false;
		private var orgFileName:String = "";

		public function FudrawWriteMediator(viewCompo:Object)
        {
            super( NAME, viewCompo );
        }

		override protected function initializeView():void
		{
			super.initializeView();
		}

		private function setEventListener():void
		{
			view.addEventListener(fudrawWrite.FudrawNoticeSave, onFudrawSave);
			view.addEventListener(fudrawWrite.FudrawNoticeCancel, onCancel);
		}

		private function removeEventListener():void
		{
			
//			view.removeEventListener(NoticeWrite.NoticeOnlyFileDelete, onNoticeOnlyFileDelete);
//			view.removeEventListener(NoticeWrite.NoticeDelete, onNoticeDelete);

			view.removeEventListener(fudrawWrite.FudrawNoticeSave, onFudrawSave);
			view.removeEventListener(fudrawWrite.FudrawNoticeCancel, onCancel);
			clearView();
			
		}

		override protected function clearView():void
		{
			//view.dataProvider = null;
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					FudrawFacade.MODE_WRITE_NOTICE_FUDRAW,
					FudrawFacade.INSERT_NOTICE_COMPLETE_FUDRAW,
					FudrawFacade.INSERT_NOTICE_FAILED_FUDRAW,
					
					
					FudrawFacade.MODE_MODIFY_NOTICE_FUDRAW,
//					,
					FudrawFacade.UPDATE_NOTICE_COMPLETE_FUDRAW,
					FudrawFacade.UPDATE_NOTICE_FAILED_FUDRAW
					];
		}



		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName())
			{
				case FudrawFacade.MODE_WRITE_NOTICE_FUDRAW:
					startWriting_FUDRAW(note.getBody() as NoticeOverViewMediator);
					break;					
				case FudrawFacade.INSERT_NOTICE_COMPLETE_FUDRAW:
					stopWriting();
					break;
				case FudrawFacade.INSERT_NOTICE_FAILED_FUDRAW:
					//CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;

				case FudrawFacade.UPDATE_NOTICE_COMPLETE_FUDRAW:
					stopWriting();
					break;
				case FudrawFacade.UPDATE_NOTICE_FAILED_FUDRAW:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;

				case FudrawFacade.MODE_MODIFY_NOTICE_FUDRAW:
					startModify(note.getBody() as NoticeOverViewMediator);
					break;					
			}
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	MVC Message handlers
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function startWriting():void
		{

		}

		private function startWriting_FUDRAW(mda:NoticeOverViewMediator):void
		{
			// 신규작성시는 dp가 업기 때문에 .. 강제로 기간 셋팅함으로 수정함 . 
//			view.dfBegda.selectedDate = mda.view.sltdfudrawVO.BEGDA_DATE;
//			view.dfEndda.selectedDate = mda.view.sltdfudrawVO.ENDDA_DATE;
			
			view.dataProvider = null;			
			_mode = MODE_WRITING;
			setEventListener();			
			view.cbFutyp.dataProvider = mda.acCode;
			 
			view.tiLTARGET.text = mda.view.lbLtaget.text;
			view.vbRp.verticalScrollPosition = 0;
			
			view.dfBegda.selectedDate = DateUtil.firstDateofThisMonth();
			view.dfEndda.selectedDate = DateUtil.lastDateofThisYear();
			view._dataProvider.BEGDA_DATE = view.dfBegda.selectedDate;
			view._dataProvider.ENDDA_DATE = view.dfEndda.selectedDate;
			
			view._dataProvider.isEditUI = true;
			
		}		


		private function startModify(mda:NoticeOverViewMediator):void
		{			
			_mode = MODE_MODIFY;
			setEventListener();
			
			view.cbFutyp.dataProvider = mda.acCode;
			view.dataProvider = mda.view.sltdfudrawVO;
		}

		private function stopWriting():void
		{
			_mode = MODE_WAITING;
 			removeEventListener();
 			clearView();
 			facade.sendNotification(FudrawFacade.MODE_LIST_NOTICE, true);
		}







		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onCancel(e:Event):void
		{
			trace("onCancel");
			removeEventListener();
			facade.sendNotification(FudrawFacade.MODE_LIST_NOTICE, false);
		}


 		private function onFudrawSave(event:Event):void
 		{
 			if(!validateFields()) {
 				return;
 			}
 			saveNotice();
 		}

		private function saveNotice():void
		{
			_bWaitingSave = false;
			var proxy:FudrawWriteProxy = facade.retrieveProxy(FudrawWriteProxy.NAME) as FudrawWriteProxy;
			
			//var voT:fudrawVO = new fudrawVO();
			var voT:Object = {};
			voT.PERNR 		= Variables.myPernr;
			voT.LTARGET		= view.tiLTARGET.text;
			voT.TARGET		= view.tiTARGET.text;
			voT.FUTYP		= view.cbFutyp.selectedItem.CODE;
			voT.BEGDA		= DateUtil.toBasicString(view._dataProvider.BEGDA_DATE);
			voT.ENDDA 		= DateUtil.toBasicString(view._dataProvider.ENDDA_DATE);
			proxy.insert(voT, view.acRpt, view._dataProvider.FUNUM);
		}
	
 		private function onNoticeDelete(event:Event):void
 		{
			var param:Object = new Object();
  			param.I_BOARD_ID 		= "06"
 			param.CONTENT_IDX		= view.writeKey;

            //파일삭제
            //deleteFile();

 			var proxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			proxy.deleteData(param);
 		}

		private function deleteFile():void
		{

		}
		//파일삭제


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function validateFields():Boolean
		{
			
			if(view.tiLTARGET.text == "") {
				CommonAlert.showMessage("Notice", "Grand 미래상를 입력하세요.");
				return false;
			}
			if(view.tiTARGET.text == "") {
				CommonAlert.showMessage("Notice", "목표를 입력하세요.");
				return false;
			}
			 
			return true;
		}

        private function get view():fudrawWrite
        {
            return viewComponent as fudrawWrite;
        }
	}
}